<template>
	<div class="bg">
		<goback title="地址管理"></goback>
		<div class="card">
			<div class="card_title">地址管理</div>
			
			<div class="tip" v-if="this.$store.getters.getUser.position==1">
				温馨提示: 作为企业的你,第一条会被视为农户的默认上门收货或你的上门出发地址,请仔细填写,认真核对
			</div>
			<div class="tip" v-else="this.$store.getters.getUser.position==0">
				温馨提示: 作为企业的你,第一条会被视为你的发货或者企业的上门地址,请仔细填写,认真核对
			</div>
			
			<div class="address_list">
				<div class="address_everycon" v-for="(item,index) in addresses">
					<div class="address_everycon_leftbox">
						<div class="address_everycon_topbox">
							<div>{{item.name}}</div>
							<div>{{item.telephone}}</div>
						</div>
						<div>{{item.address}}</div>
					</div>
					<div class="address_everycon_rightbox">
						<button @click="edit(index)">修改</button>
						<button @click="deleteDom(index)">删除</button>
					</div>
				</div>


			</div>

			<div class="add_container" v-if="AddSwitch">
				<div class="add_container_everycon">
					<label for="name">姓名:</label>
					<input type="text" id="name" v-model="new_location.name"  placeholder="姓名">
				</div>
				<div class="add_container_everycon">
					<label for="phone">手机号码:</label>
					<input type="text" id="phone" v-model="new_location.telephone"  placeholder="手机号码">
				</div>
				<div class="add_container_everycon">
					<label for="address">详细地址:</label>
					<textarea id="address" v-model="new_location.address"  placeholder="详细地址"></textarea>
				</div>
			</div>

			<button class="address_btn" @click="toggle">{{status}}</button>
			

		</div>
		

	</div>
</template>

<script>
	export default {
		name: "success",
		data() {
			return {
				addresses:[],
				status: '新增地址',
				AddSwitch: false,
				new_location: {
					id: 0,
					name: null,
					telephone: null,
					address: null
				},
				
			};
		},
		mounted() {
			this.getData();
		},
		methods: {
			getData(){
				this.axios.get(`${this.IP}/get_addresses`, {
					params: {
						token: this.getStorage("token"),
					}
				}).then(res => {
					this.addresses = res.data;
					console.log(this.addresses);
				}).catch(error => {
					// 处理错误  
					console.error('请求出错：', error);
				});;
			},
			sendData(){
				this.axios.post(`${this.IP}/set_addresses`, {
						token: this.getStorage("token"),
						jsonArray: this.addresses
				}).then(res => {
					console.log(this.addresses);
				}).catch(error => {
					// 处理错误  
					console.error('请求出错：', error);
				});;
			},
			
			toggle() {
				this.AddSwitch = !this.AddSwitch;
				if (this.AddSwitch) {
					this.status = "保存地址";
				} else {
					let obj = this.new_location;
					obj.id=this.addresses.length;
					this.addresses.push(obj);
					this.sendData();
					this.new_location = {
						id: 0,
						name: null,
						telephone: null,
						address: null
					}
					this.status = "新增地址";
				}
			},
			edit(index){
				this.new_location = this.addresses[index];
				this.addresses.splice(index, 1);;
				this.AddSwitch=true;
				this.status="保存地址";
			},
			deleteDom(index){
				this.addresses.splice(index, 1);;
				this.sendData();
			}
		}
		
	}
</script>

<style scoped>
	.tip{
		width: 80vw;
		margin: 3vw;
		font-size: 24px;
		background-color: #f1f1f1;
		border-radius: 10px;
		padding: 1vw;
	}
	.address_everycon {
		display: flex;
		flex-direction: row;
		padding: 1vw;
		margin: 1vw;
		border-bottom: 1px solid #bfbfbf;
		border-top:1px solid #bfbfbf;
	}

	.address_everycon_leftbox {
		width: 80vw;
	}

	.address_everycon_rightbox {
		width: 10vw;
	}

	.address_everycon_topbox {
		display: flex;
		flex-direction: row;
	}

	.address_everycon_rightbox button,
	.address_everycon_leftbox>div {
		border-radius: 20px;
		padding: 1vw;
	}

	.address_everycon_topbox div {
		margin-right: 30px;
	}

	.address_btn {
		width: 100%;
		padding: 2vw;
		margin-top: 3vw;
		border-radius: 20px;
	}

	.add_container {
		display: flex;
		flex-direction: column;
		justify-content: center;
	}
	.add_container_everycon{
		display: flex;
		flex-direction: row;
		justify-content: flex-start;
		align-items: center;
	}
	.add_container_everycon label{
		width: 16vw;
	}
	.add_container_everycon input,.add_container textarea{
		border: none;
		border: 1px solid #ebebeb;
		border-radius: 20px;
		padding: 1vw;
		margin: 1vw;
		width: 68vw;
	}
	
	
	
	

</style>